package noahzu.github.io.baselib.baseWidget;

import android.content.Context;
import android.support.annotation.DrawableRes;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import noahzu.github.io.baselib.R;


/**
 * Created by zujinhao on 17/6/9.
 */

public class HeaderView extends RelativeLayout {
    private ImageButton backBtn;
    private ImageButton shareBtn;
    private TextView titleText;
    private OnClickListener onBackListener;
    private OnClickListener onShareListener;


    public HeaderView(Context context) {
        this(context,null);
    }

    public HeaderView(Context context, AttributeSet attrs) {
        this(context, attrs,-1);
    }

    public HeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.header_view,this);
        backBtn = (ImageButton) findViewById(R.id.btn_back);
        shareBtn = (ImageButton) findViewById(R.id.iv_share);

        titleText = (TextView) findViewById(R.id.tv_title);
        backBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onBackListener != null){
                    onBackListener.onClick(v);
                }
            }
        });
        shareBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onShareListener != null){
                    onShareListener.onClick(view);
                }
            }
        });
    }

    public void setOnBackListener(OnClickListener onBackListener){
        if (backBtn.getVisibility() != VISIBLE){
            backBtn.setVisibility(VISIBLE);
        }
        this.onBackListener = onBackListener;
    }

    public void setOnShareListener(OnClickListener onShareListener){
        shareBtn.setVisibility(onShareListener == null?GONE:VISIBLE);
        this.onShareListener = onShareListener;
    }
    public void setOnRightListener(@DrawableRes int resId, OnClickListener onShareListener){
        shareBtn.setVisibility(onShareListener == null?GONE:VISIBLE);
        shareBtn.setImageResource(resId);
        this.onShareListener = onShareListener;
    }


    public void setTitle(String title){
        titleText.setText(title);
    }
}
